/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.utbm.tx52.stamp.ui.actionprovider;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.ActionProvider;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import fr.utbm.tx52.stamp.R;
import fr.utbm.tx52.stamp.ui.activity.Viewer;
import fr.utbm.tx52.stamp.ui.fragment.Gallery;

/**
 * ActionProvider which provide options to sort the image gallery
 *
 * @author GREA Antoine
 * @author TISSERAND Geoffrey
 */
public class SortType extends ActionProvider implements MenuItem.OnMenuItemClickListener
{

	private Activity activity;
	private MenuItem date;
	private MenuItem city;

	public SortType(Context activity)
	{
		super(activity);
		this.activity = (Activity) activity;
	}

	@Override
	public View onCreateActionView()
	{
		Log.d(this.getClass().getSimpleName(), "onCreateActionView");
		return null;
	}

	@Override
	public boolean onPerformDefaultAction()
	{
		Log.d(this.getClass().getSimpleName(), "onPerformDefaultAction");
		return super.onPerformDefaultAction();
	}

	@Override
	public boolean hasSubMenu()
	{
		Log.d(this.getClass().getSimpleName(), "hasSubMenu");
		return true;
	}

	@Override
	public void onPrepareSubMenu(SubMenu subMenu)
	{
		Log.d(this.getClass().getSimpleName(), "onPrepareSubMenu");
		subMenu.clear();
		date = subMenu.add(R.string.sort_by_date);
		date.setOnMenuItemClickListener(this);
		city = subMenu.add(R.string.sort_by_city);
		city.setOnMenuItemClickListener(this);
	}

	@Override
	public boolean onMenuItemClick(MenuItem item)
	{
		boolean asc = Gallery.sortOrder == Gallery.SortOrder.ASC;
		switch (item.getOrder())
		{
			case 0:
				if (Gallery.sort != null)
				{
					Gallery.sort.sortByDate(asc);
				}
				if (Viewer.sort != null)
				{
					Viewer.sort.sortByDate(asc);
				}
				break;
			case 1:
				if (Gallery.sort != null)
				{
					Gallery.sort.sortByCity(asc);
				}
				if (Viewer.sort != null)
				{
					Viewer.sort.sortByCity(asc);
				}
				break;
		}
		if (Gallery.instance != null)
		{
			Gallery.instance.imageAdapter.notifyDataSetChanged();
		}
		if (Viewer.instance != null)
		{
			Viewer.instance.imageAdapter.notifyDataSetChanged();
		}
		return true;
	}
}
